<?php
namespace Worker\Controller;

use Think\Controller;
use Common\Service\WXBizDataCryptAes;
use Common\Service\Curl;

class ApiController extends Controller
{
    /**
     * 通过code获取openid
     */
    public function getopenidbycode()
    {
        $code = I('code');
        $appid = 'wx1c006ea7534f25f0';//C('APP_ID');
        $secret = '59a101252bd62637684c6315c43e67ba';//C('APP_SECRET');
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $secret . "&js_code=" . $code . "&grant_type=authorization_code";
        $json = Curl::request("get", $url);
        $res = json_decode($json);
        $openid = $res->openid;
        if ($openid) {
            $this->ajaxReturn(['status' => true, 'code' => 1000, 'msg' => '查询openid成功', 'data' => $openid]);
        } else {
            $this->ajaxReturn(['status' => false, 'code' => 1001, 'msg' => '查询openid失败']);
        }
    }

    /**
     * 通过openid获取当前用户状态信息
     */
    public function nowOpenidInfo()
    {
        $openid = I('openid');
        
        //是否审核员
        $info = M("worker")->where(["openid" => $openid])->find();
        if (!$info) {
            $this->ajaxReturn(['status' => true, 'code' => 1000, 'msg' => '当前未绑定师傅']);
        } else {
            $partner_info = M('partner')->where(['id'=>$info['partner_id']])->find();
        
            $info['partner_name'] = $partner_info['name'];

            $ret = ['status' => true, 'code' => 1000, 'msg' => '用户当前已绑定师傅', 'data' => $info];
            $this->ajaxReturn($ret);
        }
    }
}